﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Answer
{
    class Section
    {
        private string m_SectionName;
        private string m_FileName;
        private string m_Message;
        
        private int m_sectionId;

        private List<answer> ansList = new List<answer>();
        private List<answer> ansList_temp = new List<answer>();

        private List<string> MessagePartnerInvite = new List<string>();
        private List<string> MessagePartnerIgnore = new List<string>();

        private Dictionary<string, int> MessagePartnerList = new Dictionary<string, int>();

        public Section(string SectionName, string FileName, string Message, int sectionId)
        {
            this.m_SectionName = SectionName;
            this.m_FileName = FileName;
            this.m_Message = Message;
            this.m_sectionId = sectionId;
        }

        public string getSectionName()
        {
            return this.m_SectionName;
        }

        public string getFileName()
        {
            return this.m_FileName;
        }

        public string getMessage()
        {
            return this.m_Message;
        }

        public int getSectionId()
        {
            return this.m_sectionId;
        }

        public void AddAnswer(answer ans)
        {
            ansList.Add(ans);
        }

        public bool Acceptinvite(string partner)
        {
            if(MessagePartnerInvite.Contains(partner))
            {
                MessagePartnerInvite.Remove(partner);
                return true;
            }else
                return false;
        }

        public bool PutOnIgnore(string partner)
        {
            if (MessagePartnerIgnore.Contains(partner))
            {
                MessagePartnerIgnore.Remove(partner);
                return true;
            }
            else
                return false;
        }

        public void Reset()
        {
            MessagePartnerList.Clear();
            MessagePartnerInvite.Clear();
            MessagePartnerIgnore.Clear();
        }

        public Dictionary<int, String> getAnswer(String messagepartner)
        {
            ansList_temp.Clear();
            int level = 0;
            Random rnd = new Random();

            if (MessagePartnerList.ContainsKey(messagepartner))
            {
                level = MessagePartnerList[messagepartner];
            }
            else
            {
                MessagePartnerList.Add(messagepartner, 0);
            }

            for (int i = 0; i < ansList.Count; i++)
            {
                if (ansList[i].getLevel() == level)
                    ansList_temp.Add(ansList[i]);
            }

            MessagePartnerList[messagepartner] += 1;

            Dictionary<int, String> ReturnDict = new Dictionary<int,String>();

            if (ansList_temp.Count > 0)
            {
                int rand = rnd.Next(0, ansList_temp.Count);

                if (ansList_temp[rand].AcceptInvite())
                    MessagePartnerInvite.Add(messagepartner);

                if (ansList_temp[rand].putOnIgnore())
                    MessagePartnerIgnore.Add(messagepartner);

                ReturnDict.Add(ansList_temp[rand].getDelay(), ansList_temp[rand].getMessage());
            }

            return ReturnDict;
        }

    }
}
